//
//  WSManager+Courses.m
//  mUBB
//
//  Created by Cosmin Stirbu on 1/17/13.
//  Copyright (c) 2013 Cosmin Stirbu. All rights reserved.
//

#import "WSManager+Courses.h"
#import "CoursesHandler.h"
#import "CourseDetailsHandler.h"
#import "CourseTypesOfClassesHandler.h"
#import "StudyGroupsHandler.h"

@implementation WSManager (Courses)

- (Ticket *)getCoursesByKeyword:(NSString *)keyWord filter:(NSString *)filter take:(NSInteger)take skip:(NSInteger)skip delegate:(id<CoursesProtocol>)delegate {
    // Ticket
    Ticket *ticket = [[Ticket alloc] initWithId:ticketId++];
    // Url
    NSMutableString *url = [NSMutableString stringWithString:[UserDefaultsManager getObjectForKey:WS_COURSES_SERVICE_KEY]];
    
    // Connection Delegate
    connectionDelegate = [[CoursesHandler alloc] initWithDelegate:delegate ticket:ticket];
    
    // Encode the keyword
    NSMutableString *unencodedString = [NSMutableString stringWithString:keyWord];
    [unencodedString replaceOccurrencesOfString:@" " withString:@"+" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [unencodedString length])];
    NSString *encodedString = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)unencodedString, NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8 );
   
    // Append parameters
    [url appendFormat:@"?keyword=%@&filter=%@&take=%d&skip=%d",encodedString, filter, take, skip];
    
    // Make the request
    [self getRequestWithUrl:url andHeaders:nil];
    
    // Set Connection Delegate to nil
    connectionDelegate = nil;
    
    return ticket;
}

- (Ticket *)getCourseDetailsForCourse:(Course *)course delegate:(id<CoursesProtocol>)delegate {
    // Ticket
    Ticket *ticket = [[Ticket alloc] initWithId:ticketId++];
    // Connection delegate
    connectionDelegate = [[CourseDetailsHandler alloc] initWithDelegate:delegate course:course ticket:ticket];
    
    // Make the request
    [self getRequestWithUrl:course.selfUrl andHeaders:nil];
    
    return ticket;
}

- (Ticket *)getTypesOfClassesForCourseCode:(NSString *)courseCode delegate:(id<CoursesProtocol>)delegate {
    // Ticket
    Ticket *ticket = [[Ticket alloc] initWithId:ticketId++];
    // Url
    NSMutableString *url = [NSMutableString stringWithString:[UserDefaultsManager getObjectForKey:WS_COURSES_SERVICE_KEY]];
    [url appendFormat:@"/%@/typesofclasses", courseCode];
    
    // Connection delegate
    connectionDelegate = [[CourseTypesOfClassesHandler alloc] initWithDelegate:delegate ticket:ticket];
    
    // Make the request
    [self getRequestWithUrl:url andHeaders:nil];
    
    // Set Connection Delegate to nil
    connectionDelegate = nil;
    
    return ticket;
    
}

- (Ticket *)getStudyGroupsForCourseCode:(NSString *)courseCode delegate:(id<CoursesProtocol>)delegate {
    // Ticket
    Ticket *ticket = [[Ticket alloc] initWithId:ticketId++];
    // Url
    NSMutableString *url = [NSMutableString stringWithString:[UserDefaultsManager getObjectForKey:WS_COURSES_SERVICE_KEY]];
    [url appendFormat:@"/%@/studygroups", courseCode];
    
    // Connection delegate
    connectionDelegate = [[StudyGroupsHandler alloc] initWithDelegate:delegate ticket:ticket];
    
    // Make the request
    [self getRequestWithUrl:url andHeaders:nil];
    
    // Set Connection Delegate to nil
    connectionDelegate = nil;
    
    return ticket;

}

@end
